﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kindohm.KSynth.Library;

namespace Kindohm.KSynth.UI
{
    public partial class NoteButton : UserControl
    {
        bool on;
        bool clicking;
        bool highlighted;

        public NoteButton()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(NoteButton_Loaded);
            this.ButtonPoint = new ButtonPoint();
            this.OffColor = Color.FromArgb(255, 200, 200, 200);
            this.HoverColor = Color.FromArgb(255, 255, 255, 255);
            this.OnColor = Color.FromArgb(255, 255, 255, 100);
            this.HighlightColor = Color.FromArgb(255, 255, 100, 200);
        }

        void NoteButton_Loaded(object sender, RoutedEventArgs e)
        {
            this.fillBrush.Color = this.OffColor;
        }

        public Color OffColor { get; set; }
        public Color HoverColor { get; set; }
        public Color OnColor { get; set; }
        public Color HighlightColor { get; set; }
        public int Octave { get; set; }
        public ButtonPoint ButtonPoint { get; set; }

        public bool Highlighted
        {
            get { return this.highlighted; }
            set
            {
                this.highlighted = value;
                this.OnHighlightedChanged();
            }
        }

        public double ButtonHeight
        {
            get { return this.rectangle.Height; }
            set
            {
                this.rectangle.Height = value;
            }
        }

        public double ButtonWidth
        {
            get { return this.rectangle.Width; }
            set
            {
                this.rectangle.Width = value;
            }
        }

        public bool On
        {
            get { return this.on; }
            set
            {
                this.on = value;
                this.OnOnChanged();
            }
        }

        public Note Note { get; set; }

        void OnHighlightedChanged()
        {
            if (this.highlighted)
            {
                this.SetColor(this.HighlightColor);
            }
            else
            {
                if (this.On)
                    this.SetColor(this.OnColor);
                else
                    this.SetColor(this.OffColor);
            }
        }

        void OnOnChanged()
        {
            if (this.On)
                this.SetColor(this.OnColor);
            else
                this.SetColor(this.OffColor);

            if (this.OnChanged != null)
                this.OnChanged(this, EventArgs.Empty);

        }

        private void rectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            this.SetColor(this.HoverColor);
        }

        private void rectangle_MouseLeave(object sender, MouseEventArgs e)
        {
            this.SetColor(this.on ? this.OnColor : this.OffColor);
            this.clicking = false;
        }

        private void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.clicking = true;
        }

        void SetColor(Color color)
        {
            this.fillBrush.Color = color;
        }

        private void rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.clicking)
            {
                this.clicking = false;

                if (!this.on)
                {
                    this.On = true;
                }
                else
                {
                    this.On = false;
                }
            }
        }

        public event EventHandler OnChanged;
    }
}
